@extends('layouts.app')
@push('script')
    @include('resources.repository._intro')
@endpush
@section('content')
    <h1 class="mb-3 font-normal">{{$repository->title}}</h1>
    @if($repository->tags)
        <div class="flex flex-wrap">
            @foreach($repository->tags as $tag)
                <div class="tag">{{$tag}}</div>
            @endforeach
        </div>
    @endif
    <div class="my-3">
        @can('repositories.create-chapter',$repository)
            <a href="{{route('repositories.chapters.create',['repository'=>$repository])}}"
               class="btn btn-default btn-white" id="create-chapter">创建章节</a>
        @endcan
        @can('repositories.fork',$repository)
            <a href="{{route('repositories.forks.create',['repository'=>$repository])}}"
               class="btn btn-default btn-white" id="create-fork">创建副本</a>
        @endcan
            <a href="{{route('repositories.issues.index',['repository'=>$repository])}}"
               class="btn btn-default btn-white" id="intro-issue">作品讨论 <span class="rounded-full bg-50 px-2">{{$repository->issues_count}}</span></a>
        <a href="{{route('repositories.forks.index',['repository'=>$repository])}}"
           class="btn btn-default btn-white" id="show-fork">查看副本 <span class="rounded-full bg-50 px-2">{{$repository->forks_count}}</span></a>
        <a href="{{route('repositories.download',['repository'=>$repository])}}"
           class="btn btn-default btn-white" id="download">下载作品</a>
        @can('repositories.pull',$repository)
            <a href="{{route('repositories.pulls.index',['repository'=>$repository])}}"
               class="btn btn-default btn-white" id="pull-request">拉取请求 <span class="rounded-full bg-50 px-2">{{$repository->pulls_count}}</span></a>
        @endcan
        @auth
            <tig-like class="btn btn-default btn-white intro-repository-like"
                      action="{{route('repositories.like',['repository'=>$repository])}}"
                      :is-liked="{{$repository->liked?'true':'false'}}"
                      :likes-count="{{$repository->likesCounter->count??0}}"></tig-like>
        @endauth
        @can('repositories.update',$repository)
            <a href="{{route('repositories.edit',['repository'=>$repository])}}"
               class="btn btn-default btn-white" id="repository-setting">作品设置</a>
        @endcan
        @if($repository->url)
            <a class="btn btn-default btn-white" href="{{$repository->url}}" id="source">查看原文</a>
        @endif
    </div>
    @if($repository->is_fork)

        <card>
            <card-block>从 <a
                        href="{{route('repositories.show',['id'=>$repository->sourceRepository->id])}}">{{$repository->sourceRepository->user->name ??'匿名用户'}}
                    ：{{$repository->sourceRepository->title}}</a>创建的副本
            </card-block>
        </card>
    @endif
    @if($repository->user)

        <p class="mb-2 text-70">作品创建者</p>
        <tig-profile-card :user="{{json_encode($repository->user)}}"></tig-profile-card>
    @endif
    @if($repository->introduction)

        <div class="mb-2 text-70">作品介绍</div>
        <card>
            <card-block>
                {!! $repository->introduction_html !!}
            </card-block>
        </card>

    @endif

    @foreach($currentChapters as $chapter)
        {{--current-user-id is for comment delete--}}
        <tig-chapter-item
                :chapter="{{json_encode($chapter,JSON_UNESCAPED_UNICODE|JSON_NUMERIC_CHECK)}}"
                :current-user-id="{{auth()->id()}}">
            @can('repositories.create-chapter',$repository)
                <a class="btn btn-white p-2 block update-chapter"
                   href="{{route('repositories.chapters.edit',['repository'=>$repository,'chapter'=>$chapter])}}">更新本章节</a>
            @endcan
            @can('repositories.switch-chapter',$repository)
                <a class="btn btn-white p-2 block switch-chapter"
                   href="{{route('repositories.sequences.index',['repository'=>$repository,'sequence'=>$chapter->pivot->sequence])}}">切换版本</a>
            @endcan
            @can('chapters.delete',$chapter)
                <tig-form action="{{route('chapters.destroy',['chapter'=>$chapter])}}" :is-delete="true">
                    <button type="submit" class="btn btn-white hover:text-danger p-2 block">删除</button>
                </tig-form>
            @endcan
        </tig-chapter-item>
    @endforeach
    @if($repository->user and $repository->user->reward)
        <p class="mb-2 text-70">{{$repository->user->name}}的赞赏码</p>

        <card>
            <img src="{{$repository->user->reward->url}}" alt="赞赏码" class="block max-w-xs mx-auto">
        </card>
    @endif
    {{$currentChapters->links()}}

    @can('repositories.create-chapter',$repository)
        <a href="{{route('repositories.chapters.create',['repository'=>$repository])}}"
           class="btn btn-default btn-primary btn-block">创建章节</a>
    @endcan

@endsection